package vip.zj.swing;

import org.junit.Test;
import vip.zj.toimage.VideoToImage;

import javax.swing.*;
import java.awt.event.*;
import java.io.File;

public class Video extends JDialog {
    private JPanel contentPane;
    private JButton buttonOK;
    private JTextField textField1;
    private JTextField textField2;
    private JButton button1;
    private JButton button2;
    private JFileChooser jfc=new JFileChooser();;

    public Video() {
        setContentPane(contentPane);
        setModal(true);
        getRootPane().setDefaultButton(buttonOK);

        buttonOK.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                onOK(e);
            }
        });
        button1.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                onButton1(e);
            }
        });
        button2.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                onButton2(e);
            }
        });
        // call onCancel() when cross is clicked
        setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
        addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent e) {
                onCancel();
            }
        });

        // call onCancel() on ESCAPE
        contentPane.registerKeyboardAction(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                onCancel();
            }
        }, KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0), JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);
    }

    private void onOK(ActionEvent e) {
//        JOptionPane.showMessageDialog(contentPane, "转换中...", "",JOptionPane.WARNING_MESSAGE);
        VideoToImage.major(textField1.getText(),textField2.getText());
        JOptionPane.showMessageDialog(contentPane, "转换完成", "",JOptionPane.WARNING_MESSAGE);

    }

    private void onCancel() {
        System.out.println(textField1.getText());
        System.out.println(textField2.getText());

    }


    private void onButton1(ActionEvent e) {
        jfc.setFileSelectionMode(1);
        int state = jfc.showOpenDialog(null);
        if (state == 1) {
            return;
        } else {
            File f = jfc.getSelectedFile();// f为选择到的目录
            textField1.setText(f.getAbsolutePath());
        }
    }
    private void onButton2(ActionEvent e) {
        jfc.setFileSelectionMode(1);
        int state = jfc.showOpenDialog(null);
        if (state == 1) {
            return;
        } else {
            File f = jfc.getSelectedFile();// f为选择到的目录
            textField2.setText(f.getAbsolutePath());
        }
    }



//    public void actionPerformed(ActionEvent e) {
//        // TODO Auto-generated method stub
//        if (e.getSource().equals(button1)) {// 判断触发方法的按钮是哪个
//            jfc.setFileSelectionMode(1);// 设定只能选择到文件夹
//            int state = jfc.showOpenDialog(null);// 此句是打开文件选择器界面的触发语句
//            if (state == 1) {
//                return;
//            } else {
//                File f = jfc.getSelectedFile();// f为选择到的目录
//                text1.setText(f.getAbsolutePath());
//            }
//        }
//        // 绑定到选择文件，先择文件事件
//        if (e.getSource().equals(button2)) {
//            jfc.setFileSelectionMode(0);// 设定只能选择到文件
//            int state = jfc.showOpenDialog(null);// 此句是打开文件选择器界面的触发语句
//            if (state == 1) {
//                return;// 撤销则返回
//            } else {
//                File f = jfc.getSelectedFile();// f为选择到的文件
//                text2.setText(f.getAbsolutePath());
//            }
//        }
//        if (e.getSource().equals(button3)) {
//            // 弹出对话框可以改变里面的参数具体得靠大家自己去看，时间很短
//            JOptionPane.showMessageDialog(null, "弹出对话框的实例，欢迎您-漆艾琳！", "提示", 2);
//        }
//    }

    public static void main(String[] args) {
        Video dialog = new Video();
        dialog.pack();
        dialog.setVisible(true);
        System.exit(0);
    }
}
